#! /usr/bin/perl

use strict;
use warnings;

use Geek::Dev::Debug(2);


# tests fallthrough and finding modules in testing dir simultaneously
use Geek::Dev::test_Debug;


###########################
# test permutations of DEBUG value
###########################

die("DEBUG not correct") unless DEBUG == 2;
die("master DEBUG not correct") unless Geek::DEBUG == 2;

package test;
BEGIN
{
	my $multval = eval "use Geek::Dev::Debug(1); use Geek::Dev::Debug(2); DEBUG;";

	# since we're running in mod_perl, this is going to have to be okay
	# therefore make sure it _doesn't_ error out
	die("Debug won't handle multiple use statements: $@") if $@;
	die("Second call to Debug doesn't override first (DEBUG is $multval)") unless $multval == 2;
	# original test below
	#die("Debug didn't catch multiple use statements")
	#		unless $@ =~ /^DEBUG already defined/;

	# OTOH, use Debug w/ no args is okay twice
	use Geek::Dev::Debug(1); use Geek::Dev::Debug;;
}

package test2;
BEGIN
{
	die("debug didn't set higher level for value: $@")
			unless eval "use Geek::Dev::Debug(2); DEBUG;" == 2;
}

package test3;
BEGIN
{
	my $val = eval "use Geek::Dev::Debug qw<OFF>; DEBUG;";
	die("debug doesn't understand OFF: $@")
			unless defined $val and $val == 0;
}

package test4;
BEGIN
{
	my $val = eval "use Geek::Dev::Debug qw<off>; DEBUG;";
	die("debug doesn't handle words in a case insensitive manner: $@")
			unless defined $val and $val == 0;
}

package test5;
BEGIN
{
	my $val = eval "use Geek::Dev::Debug qw<FALSE>; DEBUG;";
	die("debug doesn't understand FALSE: $@")
			unless defined $val and $val == 0;
}

package test6;
BEGIN
{
	my $val = eval "use Geek::Dev::Debug qw<not!>; DEBUG;";
	die("debug doesn't understand NOT!: $@")
			unless defined $val and $val == 0;
}

# test use Debug with no args and no higher package
# to do this, we'll need a whole separate Perl program
my $output = `perl -e 'use Geek::Dev::Debug; print DEBUG;'`;
die("top level empty Debug value doesn't yield 0 (actually got $output)")
		unless $output eq "0";


###########################
# Geek::Dev::Debug used to do some directory fiddling
# it shouldn't do that any more, so make sure it isn't
###########################

# shouldn't do it in debug mode
system(q{perl -e '
	BEGIN { $preinc = "@INC"; }
	use Geek::Dev::Debug(1);
	$postinc = "@INC";
	die("is still dir fiddling in debug mode")
		unless $preinc eq $postinc;
'}) and die;

# shouldn't do it out of debug mode either
system(q{perl -e '
	BEGIN { $preinc = "@INC"; }
	use Geek::Dev::Debug qw<OFF>;
	$postinc = "@INC";
	die("is still dir fiddling in production mode")
		unless $preinc eq $postinc;
'}) and die;


###########################
# make sure will pass -T
###########################

system(q{perl -Te '
	use Geek::Dev::Debug(1);
'}) and die;


print "ALL TESTS PASSED\n";
